
package com.mian.client;

import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.lang.reflect.Constructor;
import java.util.logging.Logger;

/**
 * A simple client that requests a greeting from the {@link }.
 */
public class GrpcClient {
  private static final Logger logger = Logger.getLogger(GrpcClient.class.getName());

  private final ManagedChannel channel;

  /** Construct client connecting to HelloWorld server at {@code host:port}. */
  public GrpcClient(String host, int port) {
    channel = ManagedChannelBuilder.forAddress(host, port)
            // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
            // needing certificates.
            .usePlaintext(true)
            .build();
  }

  /**
   * 获取service
   * @param type
   * @return
   */
  public Object getBlockingStub(Class<?> type) {

    try{
      Constructor c = type.getDeclaredConstructor(new Class[]{Channel.class});
      c.setAccessible(true);
      return c.newInstance(new Object[]{channel});
    }catch (Exception e){
      throw new RuntimeException("service接口不存在参数为Channel的构造函数");
    }
  }





}
